package com.wg.msshopsp.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wg.msshopsp.entity.Review;
import com.wg.msshopsp.entity.ReviewForm;
import com.wg.msshopsp.entity.ReviewUserDTO;
import com.wg.msshopsp.entity.ReviewUserShopDto;
import com.wg.msshopsp.mapper.ReviewMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ReviewService extends ServiceImpl<ReviewMapper, Review> {
    @Autowired
    private ReviewMapper reviewMapper;

    public List<ReviewUserDTO> getReviewUserInfo(Integer productId)
    {
        return reviewMapper.getReviewUserInfo(productId);
    }

    public List<ReviewUserDTO> getReviewUserInfoByTime(Integer shopId)
    {
        return reviewMapper.getReviewUserInfoByTime(shopId);
    }

    //=================================
    @Transactional(rollbackFor = Exception.class)
    public void addReview(ReviewForm form, Integer userId) {
        // 执行单SQL插入（包含所有验证）
        int rows = reviewMapper.insertReviewWithValidation(form, userId);

        // 若插入行数为0，说明验证失败
        if (rows == 0) {
            throw new RuntimeException("评价失败：订单不存在、未完成或不属于当前用户");
        }
    }
    public List<ReviewUserShopDto> getReviewUserShopInfo(Integer productId)
    {
        return reviewMapper.getReviewUserShopInfo(productId);
    }
}
